import java.io.File;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

public class Database_Populate_Local_Directory {

	// JDBC driver name and database URL
	static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
	static final String DB_URL = "jdbc:mysql://127.0.0.255:3306/iLink";
	//  Database credantials
	static final String USER = "root";
	static final String PASS = "";

	public static void main(String[] args) {
		Connection conn = null;
		PreparedStatement stmt = null;
		DirectoryStructure dir=new DirectoryStructure("C:\\Users\\Yash\\Desktop\\Sem 5");
		ArrayList<File> files=dir.getFiles();
		try{
			//STEP 2: Register JDBC driver
			Class.forName("com.mysql.jdbc.Driver");

			//STEP 3: Open a connection
			System.out.println("Connecting to database...");
			conn = DriverManager.getConnection(DB_URL,USER,PASS);

			//STEP 4: Execute a query
			int count=0;
			while(count<files.size()){
				if(files.get(count).isFile()){
					
				System.out.println("Creating statement...");
				stmt = conn.prepareStatement("insert into files values(?,?,?,?,?)");
				
				long datetime=files.get(count).lastModified();
				Date d = new Date(datetime);
		        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
		        String dateString = sdf.format(d);
		        
				stmt.setString(1, files.get(count).getName());
				stmt.setString(2, files.get(count).getAbsolutePath());
				stmt.setLong(3, files.get(count).length());
				stmt.setString(4, dateString);
				stmt.setString(5, "127.0.0.1");	
				stmt.executeUpdate();
			}
				count++;
			}
			stmt.close();
			conn.close();
		}catch(SQLException se){
			//Handle errors for JDBC
			se.printStackTrace();
		}catch(Exception e){
			//Handle errors for Class.forName
			e.printStackTrace();
		}finally{
			try{
				if(conn!=null)
					conn.close();
			}catch(SQLException se){
				se.printStackTrace();
			}//end finally try
		}//end try
		System.out.println("Goodbye!");
	}//end main


}